package com.governtalk.backend.controller;

import com.governtalk.backend.dto.CollectionDto;
import com.governtalk.backend.service.CollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/collections")
public class CollectionController {
    
    @Autowired
    private CollectionService collectionService;
    
    @PostMapping
    public ResponseEntity<CollectionDto> addCollection(@RequestBody CollectionDto collectionDto) {
        CollectionDto result = collectionService.addCollection(collectionDto);
        return ResponseEntity.ok(result);
    }
    
    @DeleteMapping("/user/{userId}/content/{contentType}/{contentId}")
    public ResponseEntity<Void> removeCollection(
            @PathVariable Long userId,
            @PathVariable String contentType,
            @PathVariable Long contentId) {
        collectionService.removeCollection(userId, contentType, contentId);
        return ResponseEntity.ok().build();
    }
    
    @GetMapping("/user/{userId}/check/{contentType}/{contentId}")
    public ResponseEntity<Boolean> isCollected(
            @PathVariable Long userId,
            @PathVariable String contentType,
            @PathVariable Long contentId) {
        boolean isCollected = collectionService.isCollected(userId, contentType, contentId);
        return ResponseEntity.ok(isCollected);
    }
    
    @GetMapping("/user/{userId}")
    public ResponseEntity<Page<CollectionDto>> getUserCollections(
            @PathVariable Long userId,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        Page<CollectionDto> result = collectionService.getUserCollections(userId, page, size);
        return ResponseEntity.ok(result);
    }
    
    @GetMapping("/user/{userId}/type/{contentType}")
    public ResponseEntity<Page<CollectionDto>> getUserCollectionsByType(
            @PathVariable Long userId,
            @PathVariable String contentType,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        Page<CollectionDto> result = collectionService.getUserCollectionsByType(userId, contentType, page, size);
        return ResponseEntity.ok(result);
    }
    
    @GetMapping("/user/{userId}/search")
    public ResponseEntity<Page<CollectionDto>> searchUserCollections(
            @PathVariable Long userId,
            @RequestParam String keyword,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        Page<CollectionDto> result = collectionService.searchUserCollections(userId, keyword, page, size);
        return ResponseEntity.ok(result);
    }
    
    @DeleteMapping("/{collectionId}")
    public ResponseEntity<Void> deleteCollection(@PathVariable Long collectionId) {
        collectionService.deleteCollection(collectionId);
        return ResponseEntity.ok().build();
    }
    
    @DeleteMapping("/user/{userId}/batch")
    public ResponseEntity<Void> batchDeleteCollections(
            @PathVariable Long userId,
            @RequestBody Long[] collectionIds) {
        collectionService.batchDeleteCollections(userId, collectionIds);
        return ResponseEntity.ok().build();
    }
    
    @DeleteMapping("/user/{userId}/clear")
    public ResponseEntity<Void> clearUserCollections(@PathVariable Long userId) {
        collectionService.clearUserCollections(userId);
        return ResponseEntity.ok().build();
    }
    
    @DeleteMapping("/user/{userId}/type/{contentType}/clear")
    public ResponseEntity<Void> clearUserCollectionsByType(
            @PathVariable Long userId,
            @PathVariable String contentType) {
        collectionService.clearUserCollectionsByType(userId, contentType);
        return ResponseEntity.ok().build();
    }
}
